package com.wm.blog_admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_admin.controller.base.AdminBaseController;
import com.wm.blog_admin.query.TMenuQuery;
import com.wm.blog_admin.service.TMenuService;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.domain.TMenuDO;
import com.wm.blog_common.entity.TMenu;
import com.wm.blog_common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wm_yu
 * @since 2020-03-24
 */
@Slf4j
@RestController
@RequestMapping("/blog/menu")
public class TMenuController extends AdminBaseController {

    @Autowired
    private TMenuService tMenuService;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<TMenuDO> get(@PathVariable Long id) {
        return tMenuService.selectById(id);
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("/delById")
    public Result delById(Long id){
        Long userId = 596078038307966976L;
        return tMenuService.deleteById(id,userId);
    }



    /**
     * 添加 菜单
     * @param sysMenu
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody TMenu sysMenu){
        Long userId = 596078038307966976L;
        return tMenuService.insert(sysMenu,userId);
    }
    /**
     * 修改
     * @param tMenu
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody TMenu tMenu){
        return tMenuService.update(tMenu);
    }

    /**
     * 分页查询
     * @param current   查询页
     * @param size   每页显示条数
     * @param query 查询参数
     * @return
     */
    @GetMapping("/page")
    public Result<Page<TMenuDO>> page(Integer current, Integer size, TMenuQuery query){
        current = Optional.ofNullable(current).orElse(1);
        size = Optional.ofNullable(size).orElse(10);
        query = Optional.ofNullable(query).orElse(new TMenuQuery());
        return tMenuService.page(new QueryPage(current,size),query);
    }


    @GetMapping("/currentMenu")
    public Result currentMenu(HttpServletRequest request){
        Long adminId = super.currentAdminId(request);
        return tMenuService.currentMenu(adminId);
    }


    /**
     * 查询所有的菜单,未被禁用的
     * @return
     */
    @GetMapping("/allMenuIvTree")
    public Result allMenuIvTree(Long roleId){
        return tMenuService.allMenuIvTree(roleId);
    }



    /**
     * 菜单排序上移
     * @return
     */
    @GetMapping("/moveSort")
    public Result moveSort(Long id,Boolean upFlag,Long parentId){
        return tMenuService.moveSort(id,upFlag,parentId);
    }


    /**
     * id查询菜单信息
     * @return
     */
    @GetMapping("/getById")
    public Result getById(Long id){
        return tMenuService.getById(id);
    }
}
